package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Department;
import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.qo.EmployeeObjectQuery;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IDepartmentService;
import cn.wolfcode.wms.service.IEmployeeService;
import cn.wolfcode.wms.service.IRoleService;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.util.PageResult;
import cn.wolfcode.wms.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    @RequestMapping("list")
    @RequirePermission("查看员工列表")
    public String list(Model model, @ModelAttribute("qo")EmployeeObjectQuery qo){
        PageResult result = employeeService.queryForList(qo);
        List<Department> depts = departmentService.selectall();
        model.addAttribute("depts",depts);
        model.addAttribute("result",result);
        return "employee/list";
    }

    @RequestMapping("input")
    @RequirePermission("员工编辑")
    public String input(Model model,Long id){
        List<Department> depts = departmentService.selectall();
        model.addAttribute("depts",depts);
        List<Role> roles = roleService.selectall();
        model.addAttribute("roles",roles);
        if (id != null) {
            Employee entity = employeeService.get(id);
            model.addAttribute("entity",entity);
        }
        return "employee/input";
    }
    @RequirePermission("员工新增或更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Employee entity,Long[]roleIds){
       employeeService.saveOrUpdate(entity,roleIds);
        return new JSONResult();
    }
    @ResponseBody
    @RequestMapping("delete")
    @RequirePermission("删除员工")
    public Object delete(Long id){
       employeeService.delete(id);
        JSONResult json = new JSONResult();
        return json;
    }
}
